package JAVA300.第十一章多线程;

/**
 * 2.分析threadLocal所处上下文 环境
 */

public class thread_19_others_6ThreadLocal1 {

    //默认存储object 添加泛型
   // private static ThreadLocal<Integer> threadLocal = new ThreadLocal<>();
    //更改初始值1-匿名
    /*private static ThreadLocal<Integer> threadLocal = new ThreadLocal<>(){
        @Override
        protected Object initialValue() {
            return 200;
        }
    };*/
    ///更改初始值2-lamda
    private static ThreadLocal<Integer> threadLocal = ThreadLocal.withInitial(()->{
        return 1;
    });

    public static void main(String[] args) {

        new Thread(new MyRun()).start();//开启五个线程 但五个线程的left不影响

    }

    public static class MyRun implements Runnable{
        //是由main引进来的，所以这里的threadLocal是main
        public MyRun() {
            threadLocal.set(1000);
            System.out.println(Thread.currentThread().getName()+"-->"+threadLocal.get());
        }


        //这部分是线程start开启之后开始的，所以这里的threadLocal是线程MyRun

        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName()+"-->"+threadLocal.get());

        }
    }
}
